package com.liveneo.adapter.outinterface.util;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.liveneo.adapter.base.entity.PrpmCaseInfo;
import com.liveneo.adapter.base.entity.PrpmCaseInfoCyp;
import com.liveneo.adapter.base.entity.PrpmLossItem;
import com.liveneo.adapter.base.entity.PrpmLossItemCyp;
import com.liveneo.adapter.base.entity.PrpmLossItemCypHistory;
import com.liveneo.adapter.base.entity.PrpmLossItemHistory;
import com.liveneo.adapter.base.entity.PrpmOrg;
import com.liveneo.adapter.base.entity.PrpmSurveyUser;
import com.liveneo.adapter.base.entity.PrpmUploadImage;
import com.liveneo.adapter.base.entity.PrpmUploadLossItemImg;
import com.liveneo.adapter.base.entity.PrpmUploadTask;
import com.liveneo.adapter.base.entity.PrpmUploadVideo;
import com.liveneo.adapter.base.entity.PrpmVerifyResultInfo;
import com.liveneo.adapter.mobile.service.CaseInfoService;
import com.liveneo.adapter.mobile.service.LossItemService;
import com.liveneo.adapter.mobile.service.PrpmCaseInfoCypService;
import com.liveneo.adapter.mobile.service.PrpmLossItemCypHistoryService;
import com.liveneo.adapter.mobile.service.PrpmLossItemCypService;
import com.liveneo.adapter.mobile.service.PrpmLossItemHistoryService;
import com.liveneo.adapter.mobile.service.PrpmSurveyUserService;
import com.liveneo.adapter.mobile.service.PrpmUploadLossItemImgService;
import com.liveneo.adapter.mobile.service.PrpmUploadService;
import com.liveneo.adapter.mobile.service.PrpmUploadTaskService;
import com.liveneo.adapter.mobile.service.VerifyResultInfoService;
import com.liveneo.adapter.outinterface.web.platform.request.AddSurvry;
import com.liveneo.adapter.outinterface.web.platform.request.DelegateLoss;
import com.liveneo.adapter.outinterface.web.platform.request.DelegateLossInfo;
import com.liveneo.adapter.outinterface.web.platform.request.InjuryTaskDis;
import com.liveneo.adapter.outinterface.web.platform.request.LossItemHistory;
import com.liveneo.adapter.outinterface.web.platform.request.SurveyRequest;
import com.liveneo.adapter.outinterface.web.platform.request.VerifyResultInfo;
import com.liveneo.system.service.OrgService;


/**
  * @Description: 数据库工具
  * @author liveneo-shuqw
  * @date 2016年9月26日 下午9:05:20
  * Copyright: Copyright (c) 2016 
  * Company: 北京朗泰
  * @version 3.0
  */
@Component
public class DBTableUtil {

	
	@Autowired
	private CaseInfoService caseInfoService;
	
	@Autowired
	private PrpmCaseInfoCypService caseInfoCypService; 
	
	@Autowired
	private LossItemService lossItemService;
	
	@Autowired
	private PrpmLossItemCypService lossItemCypService;
	
	@Autowired
	private PrpmLossItemHistoryService lossItemHistoryService;
	
	@Autowired
	private PrpmSurveyUserService surveyUserService;
	
	@Autowired
	private PrpmUploadTaskService prpmUploadTaskService;
	
	@Autowired
	private PrpmLossItemCypHistoryService prpmLossItemCypHistoryService;
	
	@Autowired
	private VerifyResultInfoService verifyResultInfoService;
	
	@Autowired
	private PrpmUploadService prpmUploadService;
	
	@Autowired
	private OrgService orgService;
	
	@Autowired
	private PrpmSurveyUserService prpmSurveyUserService; 
	
	@Autowired
	private PrpmUploadLossItemImgService prpmUploadLossItemImgService;
	
	/**
	  * @Title: saveCaseInfo
	  * @Description: 新增caseInfo
	  * @param @param injuryTaskDis
	  * @param @param uuid
	  * @param @param flag
	  * @param @param code
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int saveCaseInfo(SurveyRequest sr, String orgCode,String orgId) throws Exception {
		//新增案件
		com.liveneo.adapter.base.entity.PrpmCaseInfo prpmCaseInfo = commCaseInfo(sr, orgCode, orgId);
		prpmCaseInfo.setCreateTime(new Date());
		prpmCaseInfo.setBillingType("1"); //默认
		prpmCaseInfo.setBillingStatus("2");//默认
		prpmCaseInfo.setModifyTime(new Date());
		int res = caseInfoService.save(prpmCaseInfo);
		return res;
	}

	/**
	  * @Title: commCaseInfo
	  * @Description: 案件信息
	  * @param @param sr
	  * @param @param orgCode
	  * @param @param orgId
	  * @param @return 设定文件
	  * @return com.liveneo.adapter.base.entity.PrpmCaseInfo 返回类型
	  * @throws
	  */
	private com.liveneo.adapter.base.entity.PrpmCaseInfo commCaseInfo(SurveyRequest sr, String orgCode,
			String orgId) {
		com.liveneo.adapter.base.entity.PrpmCaseInfo prpmCaseInfo = new com.liveneo.adapter.base.entity.PrpmCaseInfo();
		prpmCaseInfo.setCaseInfoId(sr.getCaseUuid());
		prpmCaseInfo.setCaseNo(sr.getCaseNo());
		prpmCaseInfo.setOrgCode(orgCode);
		prpmCaseInfo.setOrgId(orgId);
		prpmCaseInfo.setDamagePlace(sr.getDamageplace());
		prpmCaseInfo.setDamageDriver(sr.getDrivername());
		prpmCaseInfo.setDamageDate(DateUtil.parseDateTime(sr.getDamagedate()));
		prpmCaseInfo.setDamageRemark(sr.getDamageReamrk());
		prpmCaseInfo.setUserName(sr.getUserName());
		prpmCaseInfo.setUserPhone(sr.getUserPhone());
		prpmCaseInfo.setDelegateReamrk(sr.getDelegateReamrk());
		prpmCaseInfo.setContact(sr.getContact());
		prpmCaseInfo.setContactPhone(sr.getContactPhone());
		prpmCaseInfo.setSurveyPlace(sr.getSurveyPlace());
		prpmCaseInfo.setReportDate(DateUtil.parseDateTime(sr.getRegistDate()));
		prpmCaseInfo.setCaseStatus(Integer.valueOf(sr.getCaseStatus()));
		prpmCaseInfo.setCaseDealType(sr.getCaseDealType());
		prpmCaseInfo.setLicenseNo(sr.getLicenseNo());
		return prpmCaseInfo;
	}
	
	/**
	  * @Title: saveCaseInfoCyp
	  * @Description: 新增人伤caseInfo
	  * @param @param injuryTaskDis
	  * @param @param caseUuid
	  * @param @param flag
	  * @param @param code
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int saveCaseInfoCyp(InjuryTaskDis it,String companyflag,String orgCode,String caseInfoCypId) throws Exception{
		// 新增人伤信息
		com.liveneo.adapter.base.entity.PrpmCaseInfoCyp caseInfoCyp = commCaseInfoCyp(it, companyflag, orgCode,
				caseInfoCypId);
		caseInfoCyp.setCreateTime(new Date());
		caseInfoCyp.setModifyTime(new Date());
		int res = caseInfoCypService.save(caseInfoCyp);
		return res;
	}

	/**
	  * @Title: commCaseInfoCyp
	  * @Description: 
	  * @param @param it
	  * @param @param companyflag
	  * @param @param orgCode
	  * @param @param caseInfoCypId
	  * @param @return 设定文件
	  * @return com.liveneo.adapter.base.entity.PrpmCaseInfoCyp 返回类型
	  * @throws
	  */
	private com.liveneo.adapter.base.entity.PrpmCaseInfoCyp commCaseInfoCyp(InjuryTaskDis it, String companyflag,
			String orgCode, String caseInfoCypId) {
		com.liveneo.adapter.base.entity.PrpmCaseInfoCyp caseInfoCyp = new com.liveneo.adapter.base.entity.PrpmCaseInfoCyp();
		caseInfoCyp.setCaseInfoCypId(caseInfoCypId);
		caseInfoCyp.setCaseNo(it.getCaseNo());
		caseInfoCyp.setTaskSerialNo(it.getLossID());
		//caseInfoCyp.setCompanyCode(orgCode);
		caseInfoCyp.setOrgCode(orgCode);
		caseInfoCyp.setCompanyFlag(companyflag);
		caseInfoCyp.setDamageDate(DateUtil.parseDateTime(it.getDamageDate()));
		caseInfoCyp.setDamagePlace(it.getDamagePlace());
		caseInfoCyp.setDelegateReamrk(it.getDelegateReamrk());
		caseInfoCyp.setInsuranceClass(it.getInsuranceClass());
		caseInfoCyp.setInsuranceType(it.getInsuranceType());
		caseInfoCyp.setLicenseNo(it.getLicenseNo());
		caseInfoCyp.setPolicyCode(it.getPolicyCode());
		caseInfoCyp.setPolicyHolder(it.getPolicyHolder());
		caseInfoCyp.setRemark(it.getRemark());
		caseInfoCyp.setUnitCode(it.getUnitCode());
		caseInfoCyp.setCreateBy(it.getCreateUser());
		caseInfoCyp.setModifyBy(it.getUpdateUser());
		caseInfoCyp.setModifyTime(new Date());
		return caseInfoCyp;
	}
	
	
	/**
	  * @Title: saveLossItem
	  * @Description: 新增损失项
	  * @param @param dl
	  * @param @param caseNo
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public  int saveLossItem(DelegateLoss dl, String caseNo,String caseInfoId) throws Exception {
		
		com.liveneo.adapter.base.entity.PrpmLossItem lossItem = commLossItem(dl, caseNo, caseInfoId);
		lossItem.setLossDealType("0"); //默认
		lossItem.setLossitemStatus(0); //默认
		lossItem.setCreateTime(new Date());
		lossItem.setModifyTime(new Date());
		int res = lossItemService.save(lossItem);
		
		return res;
	}

	/**
	  * @Title: commLossItem
	  * @Description: 
	  * @param @param dl
	  * @param @param caseNo
	  * @param @param caseInfoId
	  * @param @return 设定文件
	  * @return com.liveneo.adapter.base.entity.PrpmLossItem 返回类型
	  * @throws
	  */
	private com.liveneo.adapter.base.entity.PrpmLossItem commLossItem(DelegateLoss dl, String caseNo,
			String caseInfoId) {
		com.liveneo.adapter.base.entity.PrpmLossItem lossItem = new com.liveneo.adapter.base.entity.PrpmLossItem();
		lossItem.setLossItemId(dl.getLossUuid());
		lossItem.setCaseNo(caseNo);
		lossItem.setCaseInfoId(caseInfoId);
		lossItem.setLossId(dl.getLossId());
		lossItem.setLossName(dl.getLossName());
		if(StringUtils.isNotBlank(dl.getLossType())){
			lossItem.setLossType(Integer.valueOf(dl.getLossType()));
		}
		if(StringUtils.isNotBlank(dl.getLossStatus())){
			lossItem.setLossStatus(Integer.valueOf(dl.getLossStatus()));
		}
		lossItem.setSurveyorCode(dl.getSurveyorCode());
		lossItem.setSurveyorName(dl.getSurveyorName());
		lossItem.setMobile(dl.getMobile());
		lossItem.setContact(dl.getContact());
		lossItem.setContactPhone(dl.getContactPhone());
		lossItem.setUserType(Integer.valueOf(dl.getLossSource()));
		lossItem.setCheckLossUsercode(dl.getSurveyUnitCode());
		lossItem.setCheckLossUsername(dl.getSurveyUnitName());
		lossItem.setOrgCode(dl.getOrgCode());
		lossItem.setOrgName(dl.getOrgName());
		return lossItem;
	}
	
	
	/**
	  * @Title: saveLossItemCyp
	  * @Description: 新增lossItemCyp
	  * @param @param it
	  * @param @return caseInfoCypId 案件编号
	  * 			   lossItemCypId 任务编号
	  * @return int 
	  * @throws Exception
	 */
	public int saveLossItemCyp(InjuryTaskDis it,String lossItemCypId,String caseInfoCypId) throws Exception{
		//人伤任务
		PrpmLossItemCyp lossItemCyp = commLossItemCyp(it, lossItemCypId, caseInfoCypId);
		lossItemCyp.setModifyTime(new Date());
		int res = lossItemCypService.save(lossItemCyp);
		return res;
	}

	/**
	  * @Title: commLossItemCyp
	  * @Description: 
	  * @param @param it
	  * @param @param lossItemCypId
	  * @param @param caseInfoCypId
	  * @param @return 设定文件
	  * @return PrpmLossItemCyp 返回类型
	  * @throws
	  */
	private  PrpmLossItemCyp commLossItemCyp(InjuryTaskDis it, String lossItemCypId, String caseInfoCypId) {
		//人伤材料信息 ;分割
		StringBuffer sb = new StringBuffer();
		String injuryContent = null;
		if(it.getVerifyList() != null){
			for (String str : it.getVerifyList()) {
				sb.append(str).append(";");
			}
			injuryContent = sb.substring(0, sb.lastIndexOf(";"));
		}
		//处理性别
		String sex = StringUtils.equals("M", it.getInjurySex())?"男":"女";
		String mobile = StringUtils.equals("9", it.getLossType())?"":it.getCollectorPhone();
		PrpmLossItemCyp lossItemCyp = new PrpmLossItemCyp();
		lossItemCyp.setLossItemCypId(lossItemCypId);
		lossItemCyp.setCaseInfoCypId(caseInfoCypId);
		lossItemCyp.setCaseNo(it.getCaseNo());
		if(StringUtils.isNotBlank(it.getLossType())){
			lossItemCyp.setLossType(Integer.valueOf(it.getLossType()));
		}
		lossItemCyp.setVehicleModle(it.getVehicleModle());
		lossItemCyp.setLossId(it.getLossID());
		lossItemCyp.setSurveyorCode(it.getSurveyorCode());
		lossItemCyp.setSurveyorName(it.getSurveyorName());
		lossItemCyp.setSurveyUnitCode(it.getSurveyUnitCode());
		lossItemCyp.setMobile(mobile);
		lossItemCyp.setRemark(it.getRemark());
		lossItemCyp.setAuditorCode(it.getAuditorCode());
		lossItemCyp.setAuditorName(it.getAuditorName());
		lossItemCyp.setClaimStatus(it.getClaimStatus());
		lossItemCyp.setHandleStatus(it.getHandleStatus());
		if(StringUtils.isNotBlank(it.getInjuryIDType())){
			lossItemCyp.setInjuryIdType(Integer.valueOf(it.getInjuryIDType()));
		}
		lossItemCyp.setInjurySex(sex);
		lossItemCyp.setInformation(it.getInformation());
		lossItemCyp.setCollectTime(it.getCollectTime());
		lossItemCyp.setCollectAddress(it.getCollectAddress());
		if(StringUtils.isNotBlank(it.getVerifyTime())){
			lossItemCyp.setVerifyTime(Integer.valueOf(it.getVerifyTime()));
		}
		lossItemCyp.setInjuryName(it.getInjuryName());
		lossItemCyp.setObjectName(it.getObjectName());
		if(StringUtils.isNotBlank(it.getInjuryAge())){
			lossItemCyp.setInjuryAge(Integer.valueOf(it.getInjuryAge()));
		}
		lossItemCyp.setInjuryIdNo(it.getInjuryIDNo());
		lossItemCyp.setContact(it.getContact());
		lossItemCyp.setContactPhone(it.getContactPhone());
		lossItemCyp.setInjuryContent(injuryContent);
		lossItemCyp.setCreateBy(it.getCreateUser());
		lossItemCyp.setCreateTime(new Date());
		lossItemCyp.setModifyBy(it.getUpdateUser());
		return lossItemCyp;
	}
	
	
	/**
	  * @Title: saveLossItemHistory
	  * @Description: 保存任务历史轨迹
	  * @param @param caseNo
	  * @param @param caseUuid
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int saveLossItemHistory(LossItemHistory lh) throws Exception{
		PrpmLossItemHistory lossItemHistory = new PrpmLossItemHistory();
		lossItemHistory.setLossItemHistoryId(UUIDUtils.uuid());
		lossItemHistory.setLossItemId(lh.getLossItemId());
		lossItemHistory.setCaseNo(lh.getCaseNo());
		lossItemHistory.setLossType(Integer.valueOf(lh.getLossType()));
		lossItemHistory.setLossId(lh.getLossId());
		lossItemHistory.setReassignPlat(3);
		lossItemHistory.setOperateTime(new Date());
		lossItemHistory.setUserCode(lh.getUserCode());
		lossItemHistory.setOptType(lh.getOptType());
		lossItemHistory.setReassignMark(lh.getReassignMark());
		lossItemHistory.setCreateTime(new Date());
		lossItemHistory.setCaseInfoId(lh.getCaseInfoId());
		lossItemHistory.setUserType(lh.getUserType());
		int res = lossItemHistoryService.save(lossItemHistory);
		return res;
	}
	
	/**
	  * @Title: savePrpmSurveyUser
	  * @Description: 添加查勘员
	  * @param @param addSurvry
	  * @param @return 
	  * @return int 
	  * @throws
	 */
	public int savePrpmSurveyUser(AddSurvry ads,String orgId) throws Exception{
		//新增查勘员
		com.liveneo.adapter.base.entity.PrpmSurveyUser surveyUser = commSurveyUser(ads, orgId);
		surveyUser.setCreateTime(new Date());
		int res = surveyUserService.saveSurveyUser(surveyUser);
		return res;
	}

	/**
	  * @Title: commSurveyUser
	  * @Description: 
	  * @param @param ads
	  * @param @param orgId
	  * @param @return 设定文件
	  * @return com.liveneo.adapter.base.entity.PrpmSurveyUser 返回类型
	  * @throws
	  */
	private com.liveneo.adapter.base.entity.PrpmSurveyUser commSurveyUser(AddSurvry ads, String orgId) {
		com.liveneo.adapter.base.entity.PrpmSurveyUser surveyUser = new com.liveneo.adapter.base.entity.PrpmSurveyUser();
		surveyUser.setSurveyUserId(UUIDUtils.uuid());
		surveyUser.setSurveyName(ads.getName());
		surveyUser.setSurveyUserCode(ads.getUserCode());
		surveyUser.setSurveyUserType(ads.getUserType());
		surveyUser.setMobile(ads.getPhone());
		surveyUser.setAddress(ads.getAddress());
		surveyUser.setOrgCode(ads.getOrgCode());
		surveyUser.setOrgId(orgId);
		surveyUser.setCarType(ads.getCarType());
		surveyUser.setPassword(Encryption.encryptMD5ToString(Constants.PASSWORD));
		surveyUser.setValidStatus(ads.getStatus());
		return surveyUser;
	}
	
	/**
	  * @Title: saveUploadTask
	  * @Description: 新增定时上传任务
	  * @param @param caseNo
	  * @param @param caseUuid
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int saveUploadTask(String caseInfoId,String caseNo,String lossItemId,String flag) throws Exception{
		// 新增失败定时任务
		com.liveneo.adapter.base.entity.PrpmUploadTask prpmUploadTask = new com.liveneo.adapter.base.entity.PrpmUploadTask();
		prpmUploadTask.setUploadTaskId(UUIDUtils.uuid());
		prpmUploadTask.setCaseInfoId(caseInfoId);
		prpmUploadTask.setCaseNo(caseNo);
		prpmUploadTask.setLossItemId(lossItemId);
		prpmUploadTask.setFlag(flag);
		prpmUploadTask.setInputTime(new Date());
		prpmUploadTask.setUpState("0");
		prpmUploadTask.setUpTimes(0);
		prpmUploadTask.setUpType("0");
		prpmUploadTask.setCreateTime(new Date());
		int res = prpmUploadTaskService.save(prpmUploadTask);
		return res;
	}
	
	/**
	  * @Title: saveLossItemHistry
	  * @Description: 添加历史任务记录
	  * @param @param id
	  * @param @param lossHisId
	  * @param @return 
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int saveLossItemHistryCyp(String id,String lossHisId) throws Exception{
		//备份历史
		Map<String,String> param = new HashMap<String,String>();
		param.put("id", id);
		param.put("lossItemCypId", lossHisId);
		int res = prpmLossItemCypHistoryService.insertPrpmLossItemCypHistory(param);
		//备份历史
		return res;
	}

	/**
	  * @Title: saveVerifyResultInfo
	  * @Description: 保存人伤信息
	  * @param @param verifyResultInfos
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int saveVerifyResultInfo(VerifyResultInfo vr,String caseNo,String lossId,String lossItemCypId) throws Exception{
		// 保存人伤信息
		PrpmVerifyResultInfo prpmVerifyResultInfo = new PrpmVerifyResultInfo();
		prpmVerifyResultInfo.setVerifyResultInfoId(UUIDUtils.uuid());
		prpmVerifyResultInfo.setLossItemCypId(lossItemCypId);
		prpmVerifyResultInfo.setCaseNo(caseNo);
		prpmVerifyResultInfo.setAmount(vr.getName());
		prpmVerifyResultInfo.setLossId(lossId);
		prpmVerifyResultInfo.setName(vr.getName());
		prpmVerifyResultInfo.setStandard(vr.getStandard());
		prpmVerifyResultInfo.setCreateTime(new Date());
		prpmVerifyResultInfo.setModifyTime(new Date());
		int res = verifyResultInfoService.saveVerifyResultInfo(prpmVerifyResultInfo);
		return res;
	}
	
	/**
	  * @Title: updateCaseInofStateAndBill
	  * @Description: 更新任务状态
	  * @param @param caseNo
	  * @param @throws Exception 设定文件
	  * @return void 返回类型
	  * @throws
	 */
	public void updateCaseInofStateAndBill(String caseNo) throws Exception{
		caseInfoService.updateCaseInofStateAndBill(caseNo);
	}
	
	/**
	  * @Title: updateLossItemStatusByLossId
	  * @Description: 更新任务状态
	  * @param @param lossStatus
	  * @param @param lossId
	  * @return int 返回类型
	  * @throws Exception
	 */
	public int updateLossItemStatusByLossId(String lossStatus ,String lossId)throws Exception{
		// 更新任务状态
		com.liveneo.adapter.base.entity.PrpmLossItem lossItem = new com.liveneo.adapter.base.entity.PrpmLossItem();
		lossItem.setLossItemId(lossId);
		lossItem.setLossStatus(Integer.valueOf(lossStatus));
		lossItem.setModifyTime(new Date());
		int res = lossItemService.updateLossItemStatusById(lossItem);
		return res;
	}
	
	/**
	  * @Title: updateLossItemStatusByCaseNoLossId
	  * @Description: 更新定损状态
	  * @param @param lossItemStatus
	  * @param @param lossId
	  * @param @param caseNo
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int updateLossItemStatusByCaseNoLossId(String lossItemStatus ,String lossId,String caseNo)throws Exception{
		// 更新
		com.liveneo.adapter.base.entity.PrpmLossItem lossItem = lossItemService.getLossItemByCaseNoLossId(caseNo, lossId);
		lossItem.setLossitemStatus(Integer.valueOf(lossItemStatus));
		lossItem.setModifyTime(new Date());
		int res = lossItemService.updateLossItemStatusById(lossItem);
		return res;
	}

	/**
	  * @throws DBException 
	  * @Title: updateLossItem78
	  * @Description: 更新实体
	  * @param @param it
	  * @param @param caseUuid
	  * @param @param lossId
	  * @param @param lossUuid
	  * @return int 返回类型
	  * @throws
	 */
	public int updateLossItemCyp(InjuryTaskDis it, String lossItemCypId, String caseInfoCypId) throws Exception {
		//更新
		PrpmLossItemCyp lossItemCyp = commLossItemCyp(it, lossItemCypId, caseInfoCypId);
		lossItemCyp.setModifyTime(new Date());
		int res = lossItemCypService.updateByPrimaryKeySelective(lossItemCyp);
		return res;
	}
	
	
	/**
	  * @Title: updateCaseInfo
	  * @Description: 更新案件信息
	  * @param @param sr
	  * @param @param orgUuid
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int updateCaseInfo(SurveyRequest sr, String orgCode, String orgId, String caseInfoId)
			throws Exception {
		// 更新案件信息
		com.liveneo.adapter.base.entity.PrpmCaseInfo prpmCaseInfo = commCaseInfo(sr, orgCode, orgId);
		prpmCaseInfo.setCaseInfoId(caseInfoId);
		prpmCaseInfo.setModifyTime(new Date());
		int res = caseInfoService.updateByPrimaryKeySelective(prpmCaseInfo);
		return res;
	}
	
	/**
	  * @Title: updateCaseInfoCyp
	  * @Description: 更新案件信息
	  * @param @param it
	  * @param @param uuid
	  * @param @param code
	  * @return int 
	  * @throws
	 */
	public int updateCaseInfoCyp(InjuryTaskDis it,String companyflag, String caseInfoCypId, String orgCode) throws Exception {
		//更新
		com.liveneo.adapter.base.entity.PrpmCaseInfoCyp prpmCaseInfoCyp = commCaseInfoCyp(it, companyflag, orgCode, caseInfoCypId);
		prpmCaseInfoCyp.setModifyTime(new Date());
		int res = caseInfoCypService.update(prpmCaseInfoCyp);
		return res;
	}
	
	/**
	  * @Title: updateLossItemCypById
	  * @Description: 根据id更新任务状态
	  * @param @param handlestatus
	  * @param @param id
	  * @return int 返回类型
	  * @throws Exception
	 */
	public int updateLossItemCypStatusById(String handleStatus,String lossItemCypId) throws Exception{
		PrpmLossItemCyp lossItemCyp = new PrpmLossItemCyp();
		lossItemCyp.setHandleStatus(handleStatus);
		lossItemCyp.setModifyTime(new Date());
		lossItemCyp.setLossItemCypId(lossItemCypId);
		int res = lossItemCypService.updateByPrimaryKeySelective(lossItemCyp);
		return res;
	}
	
	
	/**
	  * @Title: updateCaseInfoStatusByCaseNo
	  * @Description: 更新案件状态
	  * @param @param status
	  * @param @param uuid
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int updateCaseInfoStatusByCaseNo(String status,String billType,String caseId) throws Exception{
		com.liveneo.adapter.base.entity.PrpmCaseInfo caseInfo = new com.liveneo.adapter.base.entity.PrpmCaseInfo();
		caseInfo.setCaseInfoId(caseId);
		caseInfo.setCaseStatus(Integer.valueOf(status));
		caseInfo.setBillingType(billType);
		caseInfo.setBillingStatus("2");
		caseInfo.setModifyTime(new Date());
		int res = caseInfoService.updateByPrimaryKeySelective(caseInfo);
		return res;
	}
	
	/**
	  * @Title: updateUploadImageById
	  * @Description: 上传图片标示更新
	  * @param @param stayUpload
	  * @param @throws Exception 
	  * @return int 返回类型
	  * @throws
	 */
	public int updateImageStayUploadById(String stayUpload,String imgId) throws Exception{
		com.liveneo.adapter.base.entity.PrpmUploadImage uploadImage = new com.liveneo.adapter.base.entity.PrpmUploadImage();
		uploadImage.setUploadImageId(imgId);
		uploadImage.setStayUpload(stayUpload);
		uploadImage.setModifyTime(new Date());
		int res = prpmUploadService.updateUploadImageStayUploadById(uploadImage);
		return res;
	}
	
	/**
	  * @Title: uploadUploadVideoById
	  * @Description: 更新视频上传标示
	  * @param @param Id
	  * @param @param stayUpload
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int updateVideoStayUploadById(String stayUpload,String uploadVideoId)throws Exception{
		com.liveneo.adapter.base.entity.PrpmUploadVideo prpmUploadVideo = new com.liveneo.adapter.base.entity.PrpmUploadVideo();
		prpmUploadVideo.setStayUpload(stayUpload);
		prpmUploadVideo.setUploadVideoId(uploadVideoId);
		prpmUploadVideo.setModifyTime(new Date());
		int res = prpmUploadService.updateUploadVideoeStayUploadById(prpmUploadVideo);
		return res;
	}
	
	
	/**
	  * @Title: updateImageLossItemCypStayUploadById
	  * @Description: 更新车易陪上传标示
	  * @param @param Id
	  * @param @param stayUpload
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int updateImageLossItemCypStayUploadById(String stayUpload,String lossItemCypImgId)throws Exception{
		PrpmUploadLossItemImg prpmUploadLossItemImg = new PrpmUploadLossItemImg();
		prpmUploadLossItemImg.setStayUpload(stayUpload);
		prpmUploadLossItemImg.setModifyTime(new Date());
		prpmUploadLossItemImg.setUploadLossItemImgId(lossItemCypImgId);
		int res = prpmUploadLossItemImgService.updateByPrimaryKey(prpmUploadLossItemImg);
		return res;
	}
	/**
	  * @Title: updateLossItemHistoryById
	  * @Description: 更新任务历史记录
	  * @param @param id
	  * @param @param reassignMsg
	  * @param @return
	  * @param @throws Exception 
	  * @return int 
	  * @throws
	 */
	public int updateLossItemHistoryById(String lossItemCypHistoryId,String reassignMsg) throws Exception{
		PrpmLossItemCypHistory history = new PrpmLossItemCypHistory();
		history.setLossItemCypHistoryId(lossItemCypHistoryId);
		history.setReassignMsg(reassignMsg);
		history.setModifyTime(new Date());
		history.setModifyBy("95505");
		int res = prpmLossItemCypHistoryService.updateLossItemCypHistroryById(history);
		return res;
	}

	
	
	
	/**
	  * @Title: updateLossItem
	  * @Description: 更新任务
	  * @param @param dl
	  * @param @param lossItemId
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int updateLossItem(DelegateLoss dl,String lossItemId,String caseInfoId) throws Exception{
		com.liveneo.adapter.base.entity.PrpmLossItem lossItem = commLossItem(dl, null, caseInfoId);
		lossItem.setLossItemId(lossItemId);
		lossItem.setModifyTime(new Date());
		int res = lossItemService.updateLossItemStatusById(lossItem);
		return res;
	}
	
	/**
	  * @Title: updateLossItemStatusById
	  * @Description: 更新任务状态
	  * @param @param dl
	  * @param @return
	  * @param @throws Exception 异常
	  * @return int 
	  * @throws
	 */
	public int updateLossItemStatusById(DelegateLossInfo dl)throws Exception{
		com.liveneo.adapter.base.entity.PrpmLossItem lossItem = new com.liveneo.adapter.base.entity.PrpmLossItem();
		lossItem.setLossName(dl.getLossName());
		lossItem.setLossType(Integer.valueOf(dl.getLossType()));
		lossItem.setLossStatus(Integer.valueOf(dl.getStatus()));
		lossItem.setModifyTime(new Date());
		lossItem.setLossItemId(dl.getLossUuid());
		lossItem.setLossId(dl.getLossId());
		int res = lossItemService.updateLossItemStatusById(lossItem);
		return res;
	}
	
	
	/**
	  * @Title: updateUploadTaskById
	  * @Description: 更新定时任务
	  * @param @param upState
	  * @param @param upTimes
	  * @param @param id
	  * @param @throws Exception 
	  * @return int 返回类型
	  * @throws
	 */
	public int updateUploadTaskById(String upState,Integer upTimes,String id)throws Exception{
		com.liveneo.adapter.base.entity.PrpmUploadTask prpmUploadTask = new com.liveneo.adapter.base.entity.PrpmUploadTask();
		prpmUploadTask.setUpState(upState);
		prpmUploadTask.setUpTimes(upTimes);
		prpmUploadTask.setUploadTaskId(id);
		prpmUploadTask.setModifyTime(new Date());
		int res = prpmUploadTaskService.update(prpmUploadTask);
		return res;
	}
	
	/**
	  * @Title: updatePrpmSurveyUser
	  * @Description: 
	  * @param @param ads
	  * @param @param orgUuid
	  * @return int 返回类型
	  * @throws
	 */
	public int updatePrpmSurveyUserById(AddSurvry ads,String orgId,String userId) throws Exception{
		com.liveneo.adapter.base.entity.PrpmSurveyUser prpmSurveyUser =	commSurveyUser(ads, orgId);
		prpmSurveyUser.setSurveyUserId(userId);
		prpmSurveyUser.setModifyTime(new Date());
		int res = surveyUserService.updateSurveyUser(prpmSurveyUser);
		return res;
	}
	
	/**
	  * @Title: deleteLossItemCypByUuid
	  * @Description: 删除原有的预审核任务
	  * @param @param uuid
	  * @return int 返回类型
	  * @throws Exception
	  */
	public int deleteLossItemCypById(String lossItemCypId) throws Exception{
		int res = verifyResultInfoService.deleteVerifyResultInfo(lossItemCypId);
		return res;
	}
	
	
	/**
	  * @Title: selectCaseInfoByNo
	  * @Description: 根据案件号查询
	  * @param @param caseNo
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return PrpmCaseInfo 返回类型
	  * @throws
	 */
	public PrpmCaseInfo selectCaseInfoByNo(String caseNo,String caseInfoId) throws Exception{
		PrpmCaseInfo prpmCaseInfo = null;
		if(StringUtils.isNotBlank(caseNo)){
			prpmCaseInfo = caseInfoService.getCaseInfo(caseNo);
		}
		if(StringUtils.isNotBlank(caseInfoId)){
			prpmCaseInfo = caseInfoService.selectByPrimaryKey(caseInfoId);
		}
		return prpmCaseInfo;
	}
	

	
	/**
	  * @Title: selectCaseInfo78ByNo
	  * @Description: 查询案件78表
	  * @param @param caseNo
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return PrpmCaseInfo78 返回类型
	  * @throws
	 */
	public PrpmCaseInfoCyp selectCaseInfoCypByNo(String caseNo) throws Exception{
		PrpmCaseInfoCyp caseInfoCyp = caseInfoCypService.selectCaseInfoCypByCaseNo(caseNo);
		return caseInfoCyp;
	}
	
	/**
	  * @Title: selectOrgByCode
	  * @Description: 根据code或ID查询组织机构
	  * @param @param code id
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return PrpmOrg 返回类型
	  * @throws
	 */
	public PrpmOrg selectOrgByCode(String orgCode, String orgId) throws Exception {
		PrpmOrg prpmOrg = null;
		if (StringUtils.isNotBlank(orgCode)) {
			prpmOrg = orgService.selectByOrgCode(orgCode);
		}
		if (StringUtils.isNotBlank(orgId)) {
			prpmOrg = orgService.queryById(orgId);
		}
		return prpmOrg;
	}
	
	/**
	  * @Title: selectSurveyUserByCode
	  * @Description: 根据code查询查勘信息
	  * @param @param code
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return PrpmSurveyUser 返回类型
	  * @throws
	 */
	public PrpmSurveyUser selectSurveyUserByCode(String userCode) throws Exception{
		PrpmSurveyUser user = prpmSurveyUserService.findUserByUserCode(userCode);
		return user;
	}
	
	/**
	  * @Title: selectSurveyUserByMobile
	  * @Description: 根据mobile查询查勘信息
	  * @param @param mobile
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return PrpmSurveyUser 返回类型
	  * @throws
	 */
	public PrpmSurveyUser selectSurveyUserByMobile(String mobile) throws Exception{
		PrpmSurveyUser user = prpmSurveyUserService.findUserByPhone(mobile);
		return user;
	}
	
	
	
	/**
	  * @Title: selectLossItemByCaseNo
	  * @Description: 根据案件号查询任务
	  * @param @param caseNo
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return List<PrpmLossItem> 返回类型
	  * @throws
	 */
	public List<PrpmLossItem> selectLossItemByCaseNo(String caseNo) throws Exception{
		List<PrpmLossItem> lossItems = lossItemService.getLossItemByCaseNo(caseNo);
		return lossItems;
	}
	
	/**
	  * @Title: selectLossItemCypCountById
	  * @Description: 获取车易赔上传图片数量
	  * @param @param lossItemCypId
	  * @param @return 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int selectLossItemCypCountById(String lossItemCypId){
		List<PrpmUploadLossItemImg> list = prpmUploadLossItemImgService.selectByLossItemCypId(lossItemCypId);
		return list.size();
	}
	
	/**
	  * @Title: selectLossItemCypCountById
	  * @Description: 获取车易赔上传图片列表
	  * @param @param lossItemCypId
	  * @param @return 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public List<PrpmUploadLossItemImg> selectLossItemCypCountByIdList(String lossItemCypId,int pageNo){
		List<PrpmUploadLossItemImg> list = prpmUploadLossItemImgService.selectByLossItemCypIdPage(lossItemCypId,String.valueOf(pageNo));
		return list;
	}
	
	/**
	  * @Title: selectLossItemByUploadImgCaseNo
	  * @Description: 根据案件号查询上传图片任务
	  * @param @param caseNo
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return List<PrpmLossItem> 返回类型
	  * @throws
	 */
	public List<PrpmUploadImage> selectUploadImgByCaseNo(String caseNo,int pageNo) throws Exception{
		List<PrpmUploadImage> uploadImgList = prpmUploadService.selectImageStayUploadByCaseNoPage(caseNo, "1", null , String.valueOf(pageNo));
		return uploadImgList;
	}
	
	/**
	  * @Title: selectUploadImgCountByCaseNo
	  * @Description: 根据案件号统计上传图片总数
	  * @param @param caseNo
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int selectUploadImgCountByCaseNo(String caseNo) throws Exception{
		List<PrpmUploadImage> uploadImgList = prpmUploadService.selectImageStayUploadByCaseNoList(caseNo, "1",null);
		return uploadImgList.size();
	}
	
	/**
	  * @Title: selectUploadImgByCaseNoLossItemId
	  * @Description: 单任务上传列表
	  * @param @param caseNo
	  * @param @param pageNo
	  * @param @param lossItemId
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return List<PrpmUploadImage> 返回类型
	  * @throws
	 */
	public List<PrpmUploadImage> selectUploadImgByCaseNoLossItemId(String caseNo,int pageNo,String lossItemId) throws Exception{
		List<PrpmUploadImage> uploadImgList = prpmUploadService.selectImageStayUploadByCaseNoPage(caseNo, "1", lossItemId, String.valueOf(pageNo));
		return uploadImgList;
	}
	/**
	  * @Title: selectUploadImgCountByCaseNoLossItemId
	  * @Description: 单任务上传
	  * @param @param caseNo
	  * @param @param lossItemId
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int selectUploadImgCountByCaseNoLossItemId(String caseNo,String lossItemId) throws Exception{
		List<PrpmUploadImage> uploadImgList = prpmUploadService.selectImageStayUploadByCaseNoList(caseNo, "1",lossItemId);
		return uploadImgList.size();
	}
	
	/**
	  * @Title: selectUploadVideoCountByCaseNo
	  * @Description: 案件号统计上传视频总数
	  * @param @param caseNo
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int selectUploadVideoCountByCaseNo(String caseNo) throws Exception{
		List<PrpmUploadVideo> videoList = prpmUploadService.selectVideoStayUploadByCaseNoList(caseNo, "1",null);
		return videoList.size();
	}
	
	/**
	  * @Title: selectUploadVideoByCaseNo
	  * @Description: 查询待上传视频
	  * @param @param caseNo
	  * @param @param pageNo
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return List<PrpmUploadVideo> 返回类型
	  * @throws
	 */
	public List<PrpmUploadVideo> selectUploadVideoByCaseNo(String caseNo,int pageNo) throws Exception{
		List<PrpmUploadVideo> videoList = prpmUploadService.selectVideoStayUploadByCaseNoPage(caseNo, "1",null,String.valueOf(pageNo));
		return videoList;
	}
	
	/**
	  * @Title: selectUploadVideoCountByCaseNoLossItemId
	  * @Description: 单任务视频上传
	  * @param @param caseNo
	  * @param @param lossItemId
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return int 返回类型
	  * @throws
	 */
	public int selectUploadVideoCountByCaseNoLossItemId(String caseNo,String lossItemId) throws Exception{
		List<PrpmUploadVideo> videoList = prpmUploadService.selectVideoStayUploadByCaseNoList(caseNo, "1",lossItemId);
		return videoList.size();
	}
	
	/**
	  * @Title: selectUploadVideoByCaseNo
	  * @Description: 单任务上传视频列表
	  * @param @param caseNo
	  * @param @param pageNo
	  * @param @param lossItemId
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return List<PrpmUploadVideo> 返回类型
	  * @throws
	 */
	public List<PrpmUploadVideo> selectUploadVideoByCaseNoLossItemId(String caseNo,int pageNo,String lossItemId) throws Exception{
		List<PrpmUploadVideo> videoList = prpmUploadService.selectVideoStayUploadByCaseNoPage(caseNo, "1", lossItemId, String.valueOf(pageNo));
		return videoList;
	}
	
	
	/**
	  * @Title: selectUploadTaskByCaseUuid
	  * @Description: 查询上传图片定时任务
	  * @param @param caseUuid
	  * @param @param uType
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return PrpmUploadTask 返回类型
	  * @throws
	 */
	public PrpmUploadTask selectUploadTaskByCaseId(String caseInfoId,String upType) throws Exception{
		PrpmUploadTask uploadTask = new PrpmUploadTask();
		uploadTask.setCaseInfoId(caseInfoId);
		uploadTask.setUpType(upType);
		uploadTask = prpmUploadTaskService.selectByPrpmUploadTask(uploadTask);
		return uploadTask;
	}
	
	
	
	/**
	  * @Title: selectLossItemByCaseNoAndLossId
	  * @Description: 查询任务洗洗脑
	  * @param @param caseNo
	  * @param @param lossId
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return PrpmLossItem 返回类型
	  * @throws
	 */
	public PrpmLossItem selectLossItemByCaseNoAndLossId(String caseNo,String lossId) throws Exception{
		PrpmLossItem lossItem = lossItemService.getLossItemByCaseNoLossId(caseNo, lossId);
		return lossItem;
	}
	/**
	  * @Title: selectLossItemCyp
	  * @Description: 查询损失项
	  * @param @param caseNo
	  * @param @param lossId
	  * @param @param losstype
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return PrpmLossItem78 返回类型
	  * @throws
	 */
	public PrpmLossItemCyp selectLossItemCyp(String caseNo,String lossId,String losstype) throws Exception{
		PrpmLossItemCyp lossItemCyp = new PrpmLossItemCyp();
		lossItemCyp.setCaseNo(caseNo);
		lossItemCyp.setLossId(lossId);
		lossItemCyp.setLossType(Integer.valueOf(losstype));
		PrpmLossItemCyp lossItemcyp = lossItemCypService.selectByLossItemCyp(lossItemCyp);
		return lossItemcyp;
	}
	/**
	  * @Title: selectCompleteLossItemCyp
	  * @Description: 查询已完成的单证任务 
	  * @param @param caseNo
	  * @param @param losstype
	  * @param @param handlestatus
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return PrpmLossItem78 返回类型
	  * @throws
	 */
	public  PrpmLossItemCyp selectCompleteLossItemCyp(String caseNo,String losstype,String handleStatus) throws Exception{
		PrpmLossItemCyp lossItemCyp = new PrpmLossItemCyp();
		lossItemCyp.setCaseNo(caseNo);
		lossItemCyp.setHandleStatus(handleStatus);
		lossItemCyp.setLossType(Integer.valueOf(losstype));
		PrpmLossItemCyp lossItemcyp = lossItemCypService.selectByLossItemCyp(lossItemCyp);
		return lossItemcyp;
	}
	
	/**
	  * @Title: selectCaseInfoLossItemCyp
	  * @Description: 查询已完成的单证任务 
	  * @param @param caseNo
	  * @param @param losstype
	  * @param @param handlestatus
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return PrpmLossItem78 返回类型
	  * @throws
	 */
	public List<PrpmLossItemCyp> selectCaseInfoLossItemCyp(String caseNo,String losstype,String handleStatus,boolean isHandles) throws Exception{
		List<PrpmLossItemCyp> lossItemCypList = null;
		PrpmLossItemCyp lossItemCyp = new PrpmLossItemCyp();
		lossItemCyp.setCaseNo(caseNo);
		lossItemCyp.setLossType(Integer.valueOf(losstype));
		lossItemCyp.setHandleStatus(handleStatus);
		if(isHandles){
			lossItemCyp.setRemark("1");
			lossItemCypList = lossItemCypService.selectByLossItemCypList(lossItemCyp);
		}else{
			lossItemCyp.setRemark("2");
			lossItemCypList = lossItemCypService.selectByLossItemCypList(lossItemCyp);
		}
		return lossItemCypList;
	}
	
	/**
	  * @Title: selectSendLossItemCyp
	  * @Description: 查询改派任务列表
	  * @param @param caseNo
	  * @param @param losstype
	  * @param @param handlestatus
	  * @param @param isHandles
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return List<PrpmLossItem78> 返回类型
	  * @throws
	 */
	public List<PrpmLossItemCyp> selectSendLossItemCyp(String caseNo,String losstype) throws Exception{
		List<PrpmLossItemCyp> lossItemCypList = null;
		PrpmLossItemCyp lossItemCyp = new PrpmLossItemCyp();
		lossItemCyp.setCaseNo(caseNo);
		lossItemCyp.setLossType(Integer.valueOf(losstype));
		lossItemCyp.setRemark("3");
		lossItemCypList = lossItemCypService.selectByLossItemCypList(lossItemCyp);
		return lossItemCypList;
	}
	
	/**
	  * @Title: selectLossItemHistoryByCaseNo
	  * @Description: 查询历史记录的id
	  * @param @param caseNo
	  * @param @param lossType
	  * @param @return
	  * @param @throws Exception 设定文件
	  * @return String 返回类型
	  * @throws
	 */
	public  String selectLossItemHistoryByCaseNo(String caseNo, String lossType) throws Exception {
		String historyId = prpmLossItemCypHistoryService.selectLossItemCypHistoryId(caseNo, lossType);
		return historyId;
	}
	
	
	
	
}
